package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //查询数据库有没有
        List<ShoppingCart> list = shoppingCartMapper.select(shoppingCart);
        //有就更新
        if (list != null && list.size() > 0){
            ShoppingCart shoppingCart2 = list.get(0);
            shoppingCart2.setNumber(shoppingCart2.getNumber()+1);
            shoppingCartMapper.update(shoppingCart2);
        }else{
            //没有就添加
            if(shoppingCartDTO.getDishId() != null){
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                Dish dish = dishMapper.getByOneId(shoppingCartDTO.getDishId());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());

            }else{
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                SetmealVO setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);
        }



    }

    /**
     * 查看购物车
     * @param currentId
     * @return
     */
    @Override
    public List<ShoppingCart> selectByUserId(Long currentId) {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(currentId).build();
        List<ShoppingCart> list = shoppingCartMapper.select(shoppingCart);
        return list;
    }

    /**
     * 清空购物车功能
     */
    @Override
    public void clean() {
        shoppingCartMapper.delete(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车
     * @param shoppingCartDTO
     */
    @Override
    public void deleteByCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
//        shoppingCartMapper.deleteByCart(shoppingCart);
        List<ShoppingCart> list = shoppingCartMapper.select(shoppingCart);
        Integer number = list.get(0).getNumber();
        list.get(0).setNumber(number-1);
        shoppingCartMapper.update(list.get(0));
        if(list.get(0).getNumber() == 0) {
            shoppingCartMapper.deleteByCart(shoppingCart);
        }
    }
}
